var util = require("../util"), Reporter = require("../reporter"), webSender = require("../common/sender"), perfModel = require("../common/perf"), env = require("./env"), currHandlerInstance = null, commonInfo = {
    sr: env.getSr(),
    vp: env.getSr()
}, WeexLogger = function(e) {
    var n = this;
    return Reporter.call(n, e), n._health = {
        errcount: 0,
        apisucc: 0,
        apifail: 0
    }, n._initialPage = e.page && util.safetyCall(e.page, [], "" + e.page) || null, 
    n.beforeSend = function(e, r) {
        "error" === e ? n._health.errcount++ : "api" === e && n._health[r.success ? "apisucc" : "apifail"]++;
    }, n.init(e), n;
};

WeexLogger.prototype = util.createObject(Reporter.prototype), util.ext(Reporter._root.dftCon, {
    uid: null,
    setUsername: null,
    page: null,
    ignoreUrlPath: null,
    ignoreApiPath: null,
    urlHelper: [ {
        rule: /\/([a-z\-_]+)?\d{2,20}/g,
        target: "/$1**"
    }, /\/$/ ],
    apiHelper: {
        rule: /\/([a-z\-_]+)?\d{2,20}/g,
        target: "/$1**"
    },
    ignoreUrlCase: !0,
    imgUrl: "https://arms-retcode.aliyuncs.com/r.png?"
}), util.ext(WeexLogger.prototype, {
    constructor: WeexLogger,
    _super: Reporter,
    onReady: function(e) {
        var n = this;
        return n.hasReady ? e() : env.isH5 && "complete" !== document.readyState ? void util.on(env.win, "load", function() {
            n.hasReady = !0, e();
        }, !0) : (n.hasReady = !0, e());
    },
    setPage: function(e, n) {
        var r = this, t = r.prevPage;
        if (!1 !== n) {
            if (!e || e === t) return r;
            r.prevPage = e, clearTimeout(r.sendPVTimmer), r.handleUnload(), r.resetPageview(), 
            r.sendPVTimmer = setTimeout(function() {
                r.sendPV();
            }, 10);
        }
        r._conf.page = e;
    },
    sendRequest: function(e) {
        var n = this._conf.sendRequest, r = this.getConfig("imgUrl");
        return "function" == typeof n ? n(e, r) : webSender(e, r);
    },
    postData: function(e, n) {
        var r = {}, t = "";
        r[n] = e[n], delete e[n], "object" == typeof e && (t = util.serialize(e));
        var i = this._conf.postRequest, o = this.getConfig("imgUrl");
        return "function" == typeof i ? i(r, o + t + "&post_res=") : this.sendRequest(e);
    },
    sendPV: function() {
        var e = this;
        e.onReady(function() {
            var n = e._conf, r = env.getPvInfo();
            r.page = e._initialPage || env.getH5Page(n.ignoreUrlPath, n.ignoreUrlCase), e._lg("pv", r, e.getConfig("pvSample"));
        });
    },
    _sendWebPerf: function() {
        var e = this;
        if (!env.isH5) return e;
        e.onReady(function() {
            var n = perfModel();
            if (n) {
                var r = e._conf;
                commonInfo.ct = n.ct, n.page = e._initialPage || env.getH5Page(r.ignoreUrlPath ? r.ignoreUrlPath : r.urlHelper, r.ignoreUrlCase), 
                e._lg("perf", n, e.getConfig("sample"));
            }
        });
    },
    sendHealth: function() {
        var e = util.ext({}, this._health);
        e.healthy = e.errcount > 0 ? 0 : 1, e.begin = Date.now();
        var n = e.begin - this.sBegin;
        e.stay = n, this._lg("health", e, 1), this._health = {
            errcount: 0,
            apisucc: 0,
            apifail: 0
        };
    },
    handleUnload: function() {
        var e = Date.now();
        if (e - this._lastUnload < 200) return this;
        this._lastUnload = e, this.sendHealth(), this.speedCache && (this._lg("speed", this.speedCache), 
        this.speedCache = null, clearTimeout(this.speedTimmer)), this.clear(!0);
    },
    activeErrHandler: function(e) {
        return currHandlerInstance && !e ? this : (currHandlerInstance = this, this);
    },
    errorHandler: function(e) {
        if (e) {
            var n = e.type;
            return "error" === n ? this.error(e.error || {
                message: e.message
            }, e) : "unhandledrejection" === n && util.T(e.reason, "Error") && util.checkAutoError(e.reason) && this.error(e.reason), 
            this;
        }
    },
    _handleError: function() {
        util.on(env.win, "error", function(e) {
            currHandlerInstance && currHandlerInstance.errorHandler(e);
        }).on(env.win, "unhandledrejection", function(e) {
            currHandlerInstance && currHandlerInstance.errorHandler(e);
        });
    },
    commonInfo: function() {
        return commonInfo.uid = env.getWeexUid(this) || "", this.session ? commonInfo.sid = this.session : commonInfo.sid = this.session = util.uu(), 
        commonInfo;
    },
    init: function(e) {
        var n = this;
        n.hasInit || (!1 !== e.enableInstanceAutoSend && (env.isH5 && (n._sendWebPerf(), 
        n.activeErrHandler(!1), n._handleError(), util.on(env.win, "beforeunload", function() {
            n.handleUnload();
        })), (n._initialPage || env.isH5) && n.sendPV()), util.win && (util.win.__hasInitBlSdk = !0), 
        n.hasInit = !0);
    }
});

var singleton = null;

WeexLogger.singleton = function(e) {
    var n = util.key;
    return singleton || (util.win && util.win[n] && util.win.__hasInitBlSdk ? singleton = util.win[n] : (singleton = new WeexLogger(e), 
    util.win && (util.win[n] = singleton))), singleton;
}, WeexLogger.createExtraInstance = function(e) {
    e && "object" == typeof e && !0 !== e.enableInstanceAutoSend && (e.enableInstanceAutoSend = !1);
    return new WeexLogger(e);
}, WeexLogger._super = Reporter, WeexLogger._root = Reporter._root, Reporter.WeexLogger = WeexLogger, 
module.exports = WeexLogger;